﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;

///Author: NHDuc
///Created date: 26/11/2013
namespace VietThuongLMS.Services
{
    public class LoaiTinTucService
    {
        public static List<LoaiTinTuc> GetLoaiTinTucList()
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<LoaiTinTuc> list = context.LoaiTinTucs.ToList();
                return list;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetLoaiTinTucList", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static LoaiTinTucViewModel GetLoaiTinTucList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                List<LoaiTinTuc> list = context.LoaiTinTucs.OrderBy(c => c.ID).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.LoaiTinTucs.Count();
                return new LoaiTinTucViewModel(list, total);
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetLoaiTinTucList", ex.StackTrace, ex.Message);
                return new LoaiTinTucViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertLoaiTinTuc(LoaiTinTuc comp)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                context.AddToLoaiTinTucs(comp);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LoaiTinTucServices::InsertLoaiTinTuc", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool Edit(LoaiTinTuc newModel)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                LoaiTinTuc model = context.LoaiTinTucs.Where(c => c.ID == newModel.ID).FirstOrDefault();
                if (model != null)
                {
                    model.TenLoaiTinTuc = newModel.TenLoaiTinTuc;
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LoaiTinTucServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static LoaiTinTuc GetLoaiTinTucById(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                return context.LoaiTinTucs.Where(c => c.ID == id).FirstOrDefault();
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LoaiTinTucServices::GetLoaiTinTucById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteLoaiTinTuc(int id)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                LoaiTinTuc obj = context.LoaiTinTucs.Where(c => c.ID == id).FirstOrDefault();
                if (obj != null)
                {
                    context.DeleteObject(obj);
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LoaiTinTucServices::DeleteLoaiTinTuc", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

    }
}